package com.turtle.security.organizates.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.turtle.security.common.constant.CommonConstant;
import com.turtle.security.common.exception.CustomException;
import com.turtle.security.common.utils.CustomerStringUtils;
import com.turtle.security.organizates.constant.SysDeptConstant;
import com.turtle.security.organizates.dto.SysDeptAddDTO;
import com.turtle.security.organizates.dto.SysDeptUpdateDTO;
import com.turtle.security.organizates.entity.SysDept;
import com.turtle.security.organizates.mapper.SysDeptMapper;
import com.turtle.security.organizates.service.ISysDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统部门表 服务实现类
 *
 * @author turtle
 * date: 2020-06-04
 */
@Service
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements ISysDeptService {
    @Autowired
    private SysDeptMapper sysDeptMapper;

    /**
     * 判断父级部门是否存在或状态是否可用
     *
     * @param parentId 父级部门id
     * @return Boolean
     */
    @Override
    public void isExistParentDept(Long parentId) {
        if (parentId > 0) {
            SysDept dept = sysDeptMapper.selectById(parentId);
            if (CustomerStringUtils.isNull(dept)) {
                throw new CustomException("父级部门不存在");
            }
            if (dept.getDelFlag().equals(CommonConstant.DELETE_YES) || dept.getStatus().equals(SysDeptConstant.CLOSE_STATUS)) {
                throw new CustomException("父级部门不可用，请重新添加");
            }
        }
    }

    /**
     * 添加部门
     *
     * @param sysDept 部门信息
     */
    @Override
    public void deptAdd(SysDeptAddDTO sysDept) {
        String ancestors = getAncestors(sysDept.getParentId());
        SysDept dept = SysDept.builder()
            .deptName(sysDept.getDeptName())
            .orderNum(sysDept.getOrderNum())
            .phone(sysDept.getPhone())
            .email(sysDept.getEmail())
            .status(sysDept.getStatus())
            .leader(sysDept.getLeader())
            .parentId(sysDept.getParentId())
            .ancestors(ancestors)
            .build();
        sysDeptMapper.insert(dept);

    }

    /**
     * 判断部门名是否存在
     *
     * @param deptName 部门名
     * @param deptId   部门id
     * @return Boolean
     */
    @Override
    public void isExistDeptName(String deptName, Long deptId) {
        QueryWrapper<SysDept> queryDept = new QueryWrapper<>();
        queryDept.lambda().eq(SysDept::getDelFlag, CommonConstant.DELETE_NO);
        queryDept.lambda().eq(SysDept::getDeptName, deptName);
        if (deptId > 0) {
            queryDept.lambda().ne(SysDept::getDeptId, deptId);
        }
        List<SysDept> dept = sysDeptMapper.selectList(queryDept);
        if (CustomerStringUtils.isNotEmpty(dept)) {
            throw new CustomException("部门名已存在，请重试");
        }
    }

    /**
     * 部门是否存在或是否删除
     *
     * @param id 部门id
     */
    @Override
    public void isExistDept(Long id) {
        SysDept dept = sysDeptMapper.selectById(id);
        if (CustomerStringUtils.isNull(dept)) {
            throw new CustomException("该部门不存在");
        }
        if (dept.getDelFlag().equals(CommonConstant.DELETE_YES)) {
            throw new CustomException("该部门不可用");
        }

    }

    /**
     * 部门更新
     *
     * @param sysDept 部门信息
     */
    @Override
    public void deptUpdate(SysDeptUpdateDTO sysDept) {
        String ancestors = getAncestors(sysDept.getParentId());
        SysDept dept = SysDept.builder()
            .deptId(sysDept.getDeptId())
            .deptName(sysDept.getDeptName())
            .orderNum(sysDept.getOrderNum())
            .phone(sysDept.getPhone())
            .email(sysDept.getEmail())
            .status(sysDept.getStatus())
            .leader(sysDept.getLeader())
            .parentId(sysDept.getParentId())
            .ancestors(ancestors)
            .build();
        sysDeptMapper.updateById(dept);

    }

    /**
     * 获取部门祖级列表
     *
     * @param parentId 父级部门id
     * @return ancestors
     */
    private String getAncestors(Long parentId) {
        String ancestors = "0";
        if (parentId > 0) {
            SysDept parentDept = sysDeptMapper.selectById(parentId);
            ancestors = parentDept.getAncestors() + "," + parentDept.getDeptId();
        }
        return ancestors;
    }

    /**
     * 删除部门
     *
     * @param deptId 部门id
     */
    @Override
    public void deptDel(Long deptId) {
        SysDept dept = SysDept.builder()
            .deptId(deptId)
            .delFlag(CommonConstant.DELETE_YES)
            .build();
        sysDeptMapper.updateById(dept);

    }

    /**
     * 判断部门是否存在下级部门
     *
     * @param deptId
     */
    @Override
    public void isExistChildren(Long deptId) {

        SysDept dept = SysDept.builder()
            .parentId(deptId)
            .delFlag(CommonConstant.DELETE_NO)
            .build();
        if (CustomerStringUtils.isNotEmpty(sysDeptMapper.selectList(Wrappers.query(dept)))) {
            throw new CustomException("该部门下存在下级部门，不能删除");
        }

    }

}
